
Public Class frmStart
    Private oTrans As New clsTransferUtility
    Private oLog As New LogHandler.clsLogger
    Private m_StartTime As String = "" 'yyyyMMddHHmmss
    Private m_StopTime As String = "" 'yyyyMMddHHmmss

    Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
        Dim o As New frmSET
        o.ShowDialog()

        If o.isSave = False Then
            Exit Sub
        End If

    End Sub


    Private Sub doTrans()
        oTrans.init(My.Settings.LogFolder)
        System.Console.WriteLine("start to transfer")
        oLog.writeLine(My.Settings.LogFolder, "Static", "Start")
        oLog.writeLine(My.Settings.LogFolder, "Static", "Transfer Time " & m_StartTime & " ~ " & m_StopTime)

StartTrans:
        Dim totalDoc As Integer = 0
        oTrans.init(My.Settings.LogFolder)
        System.Console.WriteLine("start to transfer")
        oLog.writeLine(My.Settings.LogFolder, "Static", "Start")
        Console.WriteLine(m_StartTime)
        Console.WriteLine(m_StopTime)
        oLog.writeLine(My.Settings.LogFolder, "Static", "Transfer Time " & m_StartTime & " ~ " & m_StopTime)

        While Integer.Parse(My.Settings.CurrDoc) < Integer.Parse(My.Settings.DocEnd)
            'Check FileNet Backup
            Console.WriteLine("Check FileNet Backup")
            CheckFileNetBackup()
            'Check Stop Time
            Console.WriteLine(" Check Transfer STOP")
            If Now.ToString("yyyyMMddHHmmss") > m_StopTime Then
                Console.WriteLine("Wait Start")
                waitStart()
                oLog.writeLine(My.Settings.LogFolder, "Static", "Total Doc Ct " & totalDoc)
                oLog.writeLine(My.Settings.LogFolder, "Static", "End")
                GoTo StartTrans
            End If

            oTrans.doTransferByDocNo(Integer.Parse(My.Settings.DocStart), _
                                     Integer.Parse(My.Settings.DocEnd), _
                                     Integer.Parse(My.Settings.CurrDoc))
            totalDoc = totalDoc + oTrans.docCt
        End While
        oLog.writeLine(My.Settings.LogFolder, "Static", "Total Doc Ct " & totalDoc)
        oLog.writeLine(My.Settings.LogFolder, "Static", "End")

        System.Console.WriteLine("Transfer End")

    End Sub

    Private Sub CheckFileNetBackup()
        Dim iDuration As Integer
        If Integer.TryParse(My.Settings.BackupDuration, iDuration) = False Then
            iDuration = 60
        End If
        Dim FileNetBackupStartTime As String = ""
        Dim FileNetBackupEndTime As String = ""
        FileNetBackupStartTime = Now.ToString("yyyyMMdd") & My.Settings.FileNetBackupTime.ToString.Replace(":", "")
        FileNetBackupEndTime = DateAdd(DateInterval.Minute, iDuration, CDate(Now.ToString("yyyy/MM/dd") & " " & My.Settings.FileNetBackupTime)).ToString("yyyyMMddHHmmss")

        Console.WriteLine(FileNetBackupStartTime)
        Console.WriteLine(FileNetBackupEndTime)

        If Now.ToString("yyyyMMddHHmmss") >= FileNetBackupEndTime Then
            'DO Nothing
        Else
            If Now.ToString("yyyyMMddHHmmss") >= FileNetBackupStartTime Then
                For iMinute As Integer = 1 To iDuration
                    For iSec As Integer = 1 To 60
                        System.Threading.Thread.Sleep(1000)
                    Next
                Next
            End If
        End If

    End Sub

    Private Sub GetTransferInterval()
        m_StartTime = Date.Parse(Now.ToString("yyyy/MM/dd") & " " & My.Settings.StartTime).ToString("yyyyMMddHHmmss")
        If My.Settings.StopTime < My.Settings.StartTime Then
            Select Case Weekday(Now, FirstDayOfWeek.Sunday)

                Case 6
                    m_StopTime = DateAdd(DateInterval.Day, 2, Now).ToString("yyyyMMdd") & "120000"
                Case Else
                    m_StopTime = Date.Parse(DateAdd(DateInterval.Day, 1, Now).ToString("yyyy/MM/dd") & " " & My.Settings.StopTime).ToString("yyyyMMddHHmmss")
            End Select

        Else
            m_StopTime = Date.Parse(Now.ToString("yyyy/MM/dd") & " " & My.Settings.StopTime).ToString("yyyyMMddHHmmss")
        End If

    End Sub

    Private Sub frmStart_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = Application.StartupPath & " Trans IM to CM"
    End Sub

    Private Sub waitStart()
        GetTransferInterval()
        While True
            System.Threading.Thread.Sleep(5000)
            Console.Write(".")
            If Now.ToString("yyyyMMddHHmmss") > m_StartTime Then
                Console.Clear()
                Exit While
            End If
        End While
    End Sub

    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
        doTrans()
    End Sub
End Class
